package com.beiding.workordersystem.orders.looporder.model;

import com.beiding.workordersystem.common.standard.E;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class ExpirationDayConfig {

    //默认日期,
    private Integer defaultDay;

    //特殊规则
    private String rules;

    public Map<Integer, Integer> rulesToMap() {
        HashMap<Integer, Integer> map = new HashMap<>();
        if (rules == null || rules.trim().equals("")) {
            return new HashMap<Integer, Integer>() {
                @Override
                public Integer get(Object key) {
                    return map.getOrDefault(key, defaultDay);
                }
            };
        }

        String[] split = rules.split(",");

        for (String s : split) {
            String[] strings = s.split("-");
            if (strings.length == 2) {
                try {
                    String key = strings[0].trim();
                    String value = strings[1].trim();
                    Integer m = Integer.valueOf(key.trim());
                    Integer d = Integer.valueOf(value.trim());
                    map.put(m, d);
                } catch (Exception ignore) {
                    E.error("无效的时间规则:" + s);
                }
            } else {
                E.error("无效的时间规则:" + s);
            }
        }

        return new HashMap<Integer, Integer>() {
            @Override
            public Integer get(Object key) {
                return map.getOrDefault(key, defaultDay);
            }
        };

    }

}
